package com.baofu.paycore.biz.engine.processor.impl;

import com.baofu.paycore.biz.engine.command.impl.BankCommand;
import com.baofu.paycore.biz.engine.result.CommandResult;
import com.baofu.paycore.biz.enums.CommandProcessorEnum;
import com.baofu.paycore.service.facade.RechargeServiceFacade;
import com.baofu.paycore.service.facade.enums.PayStatusEnums;
import com.baofu.paycore.service.facade.model.WebPayResDTO;
import com.baofu.paycore.service.facade.model.recharge.RechargeDTO;
import com.google.common.collect.Maps;
import com.system.commons.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.slf4j.helpers.SystemMarker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * com.baofu.paycore.web 银行处理器
 * <p>
 *     1、获取处理器类型
 *     2、执行特定支付工具的特定支付类型的批量支付命令
 *     3、执行取消方法
 * </p>
 *
 * User: LZQ Date: 2016/04/22  Version: 1.0
 */
@Slf4j
@Service
public class WebBankProcessor extends BankProcessor {

    @Autowired
    private RechargeServiceFacade rechargeService;

    /**
     * 获取处理器类型
     *
     * @return 命令处理器
     */
    @Override
    public CommandProcessorEnum getProcessorType() {
        return null;
    }

    /**
     * 执行特定支付工具的特定支付类型的批量支付命令，并返回相应的结果
     *
     * @param bankCommand 支付命令
     * @return 指令执行结果
     */
    @Override
    public Map<String, CommandResult> doProcess(BankCommand bankCommand) {
        log.debug("web 银行处理命令：commands={}", bankCommand);
        Map<String, CommandResult> result = Maps.newHashMap();
        try {
            RechargeDTO rechargeDTO = convertToRechargeDTO(bankCommand);
            log.info("统一支付充值请求参数，request={}", rechargeDTO);
            Result<WebPayResDTO> rechargeResDTO = rechargeService.webRecharge(rechargeDTO,
                    MDC.get(SystemMarker.TRACE_LOG_ID));
            log.info("统一支付充值请求结果，result={}", rechargeResDTO);

            CommandResult payResult = new CommandResult();
            payResult.setCommand(bankCommand);
            if (rechargeResDTO.isSuccess()) {
                WebPayResDTO rechargeResult = rechargeResDTO.getResult();
                payResult.setRealRtnFlag(rechargeResult.isRealRtnFlag());
                payResult.setStatus(rechargeResult.getStatus());
                payResult.setResultCode(rechargeResult.getErrorCode());
                payResult.setResultMsg(rechargeResult.getErrorMsg());
                payResult.setResponseDate(new Date());
                payResult.setResponseNo(rechargeResult.getPayNo());
                payResult.setResultVO(rechargeResult);
                result.put(bankCommand.getCommandNo(), payResult);
                return result;
            }
            //调用web充值返回异常,web支付单置为失败
            payResult.setRealRtnFlag(true);
            payResult.setStatus(PayStatusEnums.FAIL.getCode());
            payResult.setResponseDate(new Date());
            payResult.setResultCode(rechargeResDTO.getErrorCode());
            payResult.setResultMsg(rechargeResDTO.getErrorMsg());
            result.put(bankCommand.getCommandNo(), payResult);

        } catch (Exception e) {
            log.error("统一支付充值指令处理失败{}", e);
            CommandResult payResult = new CommandResult();
            payResult.setRealRtnFlag(false);
            payResult.setStatus(PayStatusEnums.FAIL.getCode());
            result.put(bankCommand.getCommandNo(), payResult);
        }
        return result;
    }

    /**
     * 执行取消方法
     *
     * @param commands 支付命令
     * @return map          指令执行结果
     */
    @Override
    public Map<String, CommandResult> doCancel(List<BankCommand> commands) {
        return null;
    }
}
